<?php

namespace app\api\model\shop;


use app\shopapi\model\ShopGoods;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

//商户投诉

class ShopComplain extends Model
{
    protected $name = 'shop_complain';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public function getComplain ()
    {
        return [
            0=>'商家不让使用优惠买单',
            1=>'服务于购买时不一致',
            2=>'商家引导用户改用非省团方式付款',
            3=>'商家存在刷销量等虚假交易行为',
            4=>'商家存在食品安全问题',
            5=>'商家涉及违法违规内容',
            6=>'其他投诉'
        ];
    }


    //投诉
    public function add($params)
    {
        if (empty($params['complain_type'])) throw new \Exception('请选择投诉类型');
        if (empty($params['complain_content'])) throw new \Exception('请输入投诉内容');
        if (empty($params['contact_way'])) throw new \Exception('请输入联系方式');

        Db::startTrans();

        try {
            $shop_complain_model = new ShopComplain();

            $data = [
                'shop_id'=>$params['shop_id'],
                'user_id'=>$params['user_id'],
                'complain_type'=>$params['complain_type'],
                'complain_content'=>$params['complain_content'],
                'contact_way'=>$params['contact_way'],
                'images'=>$params['images'],
            ];

            $res = $shop_complain_model->save($data);

            Db::commit();

            return $res;
        }  catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage());

        }
    }
}